package com.wing.order.controller.app;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.exception.BusinessException;
import com.wing.common.utils.JsonResult;
import com.wing.config.BaseDataInit;
import com.wing.enterprise.product.model.request.ProductPackLabelForm;
import com.wing.enterprise.product.model.response.ProductPackLabelVO;
import com.wing.order.model.entity.OrderDelivery;
import com.wing.order.model.entity.OrderDeliveryDetail;
import com.wing.order.model.entity.OrderReceiveLabel;
import com.wing.order.model.response.InventoryJobLabelVO;
import com.wing.warehouse.model.response.PalletBoxListVO;
import com.wing.web.utils.JwtUtils;
import com.wing.web.utils.LocaleMessage;
import com.wing.common.utils.PageResult;
import com.wing.order.model.entity.OrderDeliveryLabel;
import com.wing.order.model.response.OrderDeliveryLabelVO;
import com.wing.order.service.OrderDeliveryLabelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.wing.order.model.request.OrderDeliveryLabelForm;
import org.springframework.validation.annotation.Validated;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author
 */
@Validated
@Api(value = "App-签收单扫描的包装条码管理", tags = "签收单扫描的包装条码管理")
@RestController("appOrderDeliveryLabelController")
@RequestMapping("order/app/orderDeliveryLabel")
@Slf4j
public class OrderDeliveryLabelController {

    @Autowired
    private OrderDeliveryLabelService orderDeliveryLabelService;
    @Autowired
    private LocaleMessage localeMessage;

    @GetMapping()
    public PageResult<OrderDeliveryLabelVO> list(
            @RequestParam(name = "page", value = "第几页", defaultValue = "1", required = false) Integer page
            ,@RequestParam(name = "limit", value = "每页多少条", defaultValue = "10", required = false) Integer limit
            ,@RequestParam(name = "sort", value="排序字段名", defaultValue = "create_time", required = false) String sort
            ,@RequestParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "desc", required = false) String order
    ) {
        Page<OrderDeliveryLabelVO> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }

        Page<OrderDeliveryLabelVO> list = orderDeliveryLabelService.pageList4app(pg, params);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @GetMapping("/{id}")
    public JsonResult detail(@PathVariable("id") Long orderDeliveryLabelId) {
        OrderDeliveryLabelVO orderDeliveryLabelVO = orderDeliveryLabelService.get4appById(orderDeliveryLabelId);
        if (orderDeliveryLabelVO == null) {
            return JsonResult.fail(localeMessage.getMessage("common.fail"));
        }
        return JsonResult.success(localeMessage.getMessage("common.success"), orderDeliveryLabelVO);
    }

    @PostMapping()
    public JsonResult add(@Validated OrderDeliveryLabelForm orderDeliveryLabelForm) {
        if (orderDeliveryLabelService.add(orderDeliveryLabelForm) > 0) {
            return JsonResult.success(localeMessage.getMessage("common.success"));
        }
        return JsonResult.fail(localeMessage.getMessage("common.fail"));
    }


    @ApiOperation(value = "扫描包装条码获取包装条码信息", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "signId", value = "配送单明细id", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "labelCode", value = "条码code", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
    })
    @GetMapping("/getPackLabel")
    public JsonResult<ProductPackLabelVO> getPackLabel(Long signId, String labelCode){
        return orderDeliveryLabelService.getPackLabel(signId,labelCode);
    }


    @ApiOperation(value = "获取配送单明细料箱里面的条码数据", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "signId", value = "配送单明细id", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "boxId", value = "料箱id", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
    })
    @GetMapping("/getLabelByDeliverySignBoxId")
    public JsonResult<List<InventoryJobLabelVO>> getLabelByDeliveryDetailIdBoxId(Long signId,Long signDetailId, Long boxId,String queryFlag){
//        String queryFlag=null;
        if (boxId==null&&StringUtils.isBlank(queryFlag)){
            queryFlag="queryAllBox";
        }
        return JsonResult.success(orderDeliveryLabelService.getDeliveryLabel(signId,boxId,queryFlag,signDetailId));
    }


    @ApiOperation(value = "入库提交")
    @PostMapping("/inventoryInSubmit")
    public JsonResult inventoryImSubmit(@RequestBody List<OrderDeliveryLabel> list, HttpServletRequest request){
        try{
            Long creator= JwtUtils.getLoginUserId();
            orderDeliveryLabelService.inventoryInSubmit(list);
            return JsonResult.success("领货提交成功");
        }catch (Exception e){
            log.error("receive submit fail",e);
            return JsonResult.fail(e.getMessage());
        }
    }


    @ApiOperation(value = "获取配送单明细料箱里面的条码数据", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "signId", value = "配送单明细id", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
    })
    @GetMapping("/getBoxBySignId")
    public JsonResult<List<PalletBoxListVO>> getBoxBySignId(Long signId,Long signDetailId){
        return JsonResult.success(orderDeliveryLabelService.getBoxBySignId(signId,signDetailId));
    }


}
